// pages/article/list/index.js
const {
  schoolArticle,
  articleList,
  courseList
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 9,
    pageNo: 1,
    pageSize: 20,
    mode: 'course',
    list: [],
    type: 1,
    payType:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type: options.type
      })
      if(options.payType){
        this.setData({
          payType: options.payType
        })
      }
      this.getList()
    }
    if (options.title) {
      wx.setNavigationBarTitle({
        title: options.title,
        icon: 'none'
      })
    }
    if (options.mode) {
      this.setData({
        mode: options.mode
      })
    }
    if (options.list) {
      const list = JSON.parse(decodeURIComponent(options.list))
      this.setData({
        list
      })
    }

  },
  async getList() {
    const {
      type,
      pageNo,
      pageSize,
      payType
    } = this.data
    const params = {
      pageNo,
      pageSize,
      type
    }
    if (type == 2) {
      params.payType=payType
      var {
        result
      } = await courseList(params)
    } else {
      var {
        result
      } = await articleList(params)
    }
    if (result.records.length < 1) return
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      list,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) {
      return
    }
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const {type}=this.data
    return {
      title: '德道升学',
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg',
      path:`/pages/article/list/index?type=${type}`
    }
  },
  detail(e) {
    const url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  }
})